/**
 * 与用户相关的数据库操作
 */
package com.xiaomuwu.android.logic.dao

import android.util.Log
import com.xiaomuwu.android.logic.model.*
import com.xiaomuwu.android.ui.Displayer
import java.sql.Connection
import java.sql.ResultSet
import kotlin.concurrent.thread

object UserData {

    /**
     * 登录检查
     */
    fun checkLogin(forCheckLogin: ForCheckLogin,connection: Connection?):String?{
        val userID=if(forCheckLogin.userID==null){
            null
        }else{
            "'${forCheckLogin.userID}'"
        }
        val password=if(forCheckLogin.password==null){
            null
        }else{
            "'${forCheckLogin.password}'"
        }
        val sql="exec checkLogin $userID,$password"
        var result: ResultSet?=null
        try{
//            Thread{
                result=connection?.prepareStatement(sql)?.executeQuery()
                Log.d("UserData","checkLogin操作成功")
//            }.start()
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("UserData","checkLogin操作失败")
        }
        return if (result!=null){
            result.next()
            result.getString(1)
        }else{
            result
        }
    }

    /**
     * 注册账号
     */
    fun registerUser(forOperateUser: ForOperateUser, connection: Connection?):String?{
        val sql="exec operateUser '${forOperateUser.userID}','${forOperateUser.userName}'," +
                "'${forOperateUser.password}','${forOperateUser.action}'"
        var result:ResultSet?=null
        try{
//            Thread{
            result=connection?.prepareStatement(sql)?.executeQuery()
            Log.d("UserData","registerUser操作成功")
//            }.start()
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("UserData","registerUser操作失败")
        }
        return if (result!=null){
            result.next()
            result.getString(1)     //check(getString可以获取整形吗？)
        }else{
            result
        }
    }

    /**
     * 修改用户信息
     */
    fun modifyUser(forOperateUser: ForOperateUser,connection: Connection?){
        val sql="exec operateUser '${forOperateUser.userID}','${forOperateUser.userName}'," +
                "'${forOperateUser.password}','${forOperateUser.action}'"
        try {
            thread {
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("UserData","modifyUser操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("UserData","modifyUser操作失败")
        }
    }

    /**
     * 获取个人信息
     */
    fun getPersonal(userID: String?,connection: Connection?):PersonalData?{
        return if(userID==null) {
            null
        }else{
            val sql="exec getPersonal '$userID'"
            var result:ResultSet?=null
            try{
//                Thread{
                result=connection?.prepareStatement(sql)?.executeQuery()
                Log.d("UserData","getPersonal操作成功")
//                }.start()
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("UserData","getPersonal操作失败")
            }
            if(result!=null){
                result.next()
                PersonalData(result.getString("User_Name"),
                    result.getInt("Res_Count"),
                    result.getInt("Fans_Count"))
            }else{
                result
            }
        }
    }

    /**
     * 获取用户列表
     */
    fun getUser(forUser: ForUser,connection: Connection?):List<User>?{
        return run{
            val userName=if(forUser.userName==null){
                null
            }else{
                "'${forUser.userName}'"
            }
            val sql="exec getUser $userName,'${forUser.sortBy}'"
            var result:ResultSet?=null
            try {
//                thread{
                    result=connection?.prepareStatement(sql)?.executeQuery()
                    Log.d("UserData","getUser操作成功")
//                }
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("UserData","getUser操作失败")
            }
            if (result!=null){
                val dataResult=ArrayList<User>()
                while (result.next()){
                    dataResult.add(User(result.getString("User_ID"),
                        result.getString("User_Name")))
                }
                dataResult
            }else{
                result
            }
        }
    }

    /**
     * 获取相关用户（关注、粉丝）列表
     */
    fun getReleUser(forReleUser: ForReleUser, connection: Connection?):List<User>?{
        return run{
            val releUserName=if(forReleUser.releUserName==null){
                null
            }else{
                "'${forReleUser.releUserName}'"
            }
            lateinit var sql:String
            when(Displayer.pageStatus){
                "Follow"->{
                    sql ="exec getFollow '${forReleUser.userID}',$releUserName"
                }
                "Fans"->{
                    sql ="exec getFans '${forReleUser.userID}',$releUserName"
                }
            }
            var result:ResultSet?=null
            try {
//                thread {
                    result=connection?.prepareStatement(sql)?.executeQuery()
                    Log.d("ResData","getReleUser操作成功")
//                }
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("ResData","getReleUser操作失败")
            }
            if(result!=null){
                val dataResult=ArrayList<User>()
                when(Displayer.pageStatus){
                    "Follow"->{
                        while(result.next()){
                            dataResult.add(User(result.getString("Followed_ID"),
                                result.getString("Followed_Name")))
                        }
                    }
                    "Fans"->{
                        while(result.next()){
                            dataResult.add(User(result.getString("Fans_ID"),
                                result.getString("Fans_Name")))
                        }
                    }
                }
                dataResult
            }else{
                result
            }
        }
    }

    /**
     * 获取用户详情
     */
    fun getUserDetail(forUserDetail: ForUserDetail,connection: Connection?):UserDetail?{
        return run{
            val userID=if(forUserDetail.userID==null){
                null
            }else{
                "'${forUserDetail.userID}'"
            }
            val sql="exec getUserDetail $userID,'${forUserDetail.releUserID}'," +
                    "'''${forUserDetail.releUserID}'''"
            //check
            var result:ResultSet?=null
            try{
//                Thread{
                    result=connection?.prepareStatement(sql)?.executeQuery()
                    Log.d("UserData","getUserDetail操作成功")
//                }.start()
            }catch (e:Exception){
                e.printStackTrace()
                Log.d("UserData","getUserDetail操作失败")
            }
            if(result!=null){
                result.next()
                UserDetail(result.getInt("Res_Count"),
                    result.getInt("Fans_Count"),
                    result.getInt("IsFollowed"))
            }else{
                result
            }
        }
    }

    /**
     * 关注用户
     */
    fun followUser(forFollow: ForFollow,connection: Connection?){
        val sql="exec followUser '${forFollow.userID}','${forFollow.releUserID}'," +
                "'${forFollow.action}'"
        try {
            thread {
                connection?.prepareStatement(sql)?.executeUpdate()
                Log.d("UserData","followUser操作成功")
            }
        }catch (e:Exception){
            e.printStackTrace()
            Log.d("UserData","followUser操作失败")
        }
    }

}